package com.hurui.xiaoming.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.bean.Li_IsUserFinish_Result;
import com.hurui.xiaoming.myconfig.PamaryKeys;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyDeailInfoActivity extends BaseActivity {
    private ImageView back_iv;
    private TextView title_tv ;
    private Button next_btn ;
    private EditText edit_Name;

    private EditText edit_idcard;
    private EditText edit_invitation;
    private EditText edit_phone_pass;
    private EditText edit_address;
    private EditText edit_month_income;
    private EditText edit_weixin;
    private EditText borrowing_limit;
    private EditText zfb_et;
    private EditText first_name;
    private EditText first_phone;
    private EditText second_name;
    private EditText second_phone;

    private EditText third_name;
    private EditText third_phone;
    private EditText four_name;
    private EditText four_phone;
    private EditText five_name;
    private EditText five_phone;
    private EditText six_name;
    private EditText six_phone;
    private Map<String,String> map;
    private Li_IsUserFinish_Result.DataBean dataBean;
    private final String TAG=this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_deail_info);

        map=new HashMap<>();
        initView();

        initAction();
    }

    private void onHttp() {
        map.put(PamaryKeys.UI_ID, WriteReadSharedPrefs.readUserID(mContext)+"");

        apiSevice.onHttp_UpdateInfo(map).enqueue(new Callback<Li_IsUserFinish_Result>() {
            @Override
            public void onResponse(Call<Li_IsUserFinish_Result> call, final Response<Li_IsUserFinish_Result> response) {
                Log.e(TAG, "onResponse: "+response.body().getData().toString() );
                if (response.body().getStatus().equals("sucess")&& response.body().getData()!=null)

                dataBean=response.body().getData();
                onSuccess(response.body());


            }

            @Override
            public void onFailure(Call<Li_IsUserFinish_Result> call, Throwable t) {
                Log.e(TAG, "onFailure: "+t.getMessage() );
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        onHttp();
    }

    @Override
    public void initView() {
        back_iv = (ImageView) findViewById(R.id.back_iv);
        title_tv = (TextView) findViewById(R.id.top_title_tv);
        next_btn = (Button) findViewById(R.id.next_btn);
        title_tv.setText("身份信息");
        edit_Name = ((EditText) findViewById(R.id.name_et));
        edit_idcard = ((EditText) findViewById(R.id.idcard_et));
        edit_invitation = ((EditText) findViewById(R.id.invitationcode_ev));
        edit_phone_pass = ((EditText) findViewById(R.id.phone_pass_tv));
        edit_address = ((EditText) findViewById(R.id.address_et));
        edit_month_income = ((EditText) findViewById(R.id.monthly_income_et));
        edit_weixin = ((EditText) findViewById(R.id.weixin_et));
        borrowing_limit = ((EditText) findViewById(R.id.borrowing_limit_et));
        zfb_et = ((EditText) findViewById(R.id.zfb_et));
        first_name = ((EditText) findViewById(R.id.frist_name_et));
        first_phone = ((EditText) findViewById(R.id.frist_phone_et));

        second_name = ((EditText) findViewById(R.id.second_name_et));
        second_phone = ((EditText) findViewById(R.id.second_phone_et));

        third_name = ((EditText) findViewById(R.id.third_name_et));
        third_phone = ((EditText) findViewById(R.id.third_phone_et));

        four_name = ((EditText) findViewById(R.id.four_name_et));
        four_phone = ((EditText) findViewById(R.id.four_phone_et));

        five_name = ((EditText) findViewById(R.id.five_name_et));
        five_phone = ((EditText) findViewById(R.id.five_phone_et));

        six_name = ((EditText) findViewById(R.id.six_name_et));
        six_phone = ((EditText) findViewById(R.id.six_phone_et));


    }

    @Override
    public void initAction() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onFinish();
            }
        });

        next_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getUserInfo();
            }
        });

    }

    /**
     * 获取修改后信息
     */
    private void getUserInfo() {

        Li_IsUserFinish_Result.DataBean result = new Li_IsUserFinish_Result.DataBean();
        //判断是否为空
        //判断姓名

        if (!TextUtils.isEmpty(edit_Name.getText().toString())){

            result.setUi_name(edit_Name.getText().toString());

        }else {
            onToast("请输入你的真实姓名");
            return;
        }

        //判断身份证号码
        if (isEmpty(edit_idcard)!=null){
            result.setUi_cardid(isEmpty(edit_idcard));
        }else {
            onToast("请输入身份证号码");
            return;
        }

        if (isEmpty(edit_invitation)!=null){
            result.setUi_code(isEmpty(edit_invitation));
        }

        if (isEmpty(edit_phone_pass)==null){
            onToast("请输入手机号码");
            return;
        }else {
            result.setUi_phone(isEmpty(edit_phone_pass));

        }
        if (isEmpty(edit_address)!=null){
            result.setUi_address(isEmpty(edit_address));
        }else {
            onToast("请输入居住地");
            return;
        }
        if (isEmpty(edit_month_income)!=null){
            result.setUi_income(isEmpty(edit_month_income));
        }else{
            onToast("请输入月收入");
            return;
        }

        if (isEmpty(edit_weixin)!=null){
            result.setUi_qqwx(isEmpty(edit_weixin));
        }else{
            onToast("请输入微信或者QQ号");
            return;
        }

        if (isEmpty(borrowing_limit)!=null){
            result.setUi_limit(isEmpty(borrowing_limit));
        }else{
            onToast("请输入借贷额度");
            return;
        }

        //分期

        if (isEmpty(first_name)!=null){
            result.setUi_name1(isEmpty(first_name));
        }else{
            onToast("请输入紧急联系人");
            return;
        }

        if (isEmpty(first_phone)!=null){
            result.setUi_phone1(isEmpty(first_phone));
        }else{
            onToast("请输入紧急联系人电话");
            return;
        }

        if (isEmpty(second_name)!=null){
            result.setUi_name2(isEmpty(second_name));
        }else{
            onToast("请输入紧急联系人");
            return;
        }

        if (isEmpty(second_phone)!=null){
            result.setUi_phone2(isEmpty(second_phone));

        }else{
            onToast("请输入紧急联系人电话");
            return;
        }


        if (isEmpty(third_name)!=null){
            result.setUi_name3(isEmpty(third_name));
        }else{
            onToast("请输入紧急联系人");
            return;
        }

        if (isEmpty(third_phone)!=null){
            result.setUi_phone3(isEmpty(third_phone));

        }else{
            onToast("请输入紧急联系人电话");
            return;
        }

        if (isEmpty(four_name)!=null){
            result.setUi_name4(isEmpty(four_name));
        }else{
            onToast("请输入紧急联系人");
            return;
        }

        if (isEmpty(four_phone)!=null){
            result.setUi_phone4(isEmpty(four_phone));

        }else{
            onToast("请输入紧急联系人电话");
            return;
        }

        ///////
        if (isEmpty(five_name)!=null){
            result.setUi_name5(isEmpty(five_name));
        }

        if (isEmpty(five_phone)!=null){
            result.setUi_phone5(isEmpty(five_phone));

        }
        if (isEmpty(six_name)!=null){
            result.setUi_name6(isEmpty(six_name));
        }

        if (isEmpty(six_name)!=null){
            result.setUi_phone6(isEmpty(six_name));

        }


        if (isEmpty(zfb_et)!=null){
            result.setUi_alipay(isEmpty(zfb_et));
        }else {
            onToast("请输入支付宝账号！");
            return;
        }
       // start(result);
        commitID(result);
    }

    private void start() {
        Intent intent = new Intent(this, MyStudentAndJobActivity.class);
        intent.putExtra(PamaryKeys.UI_INFO,dataBean);
        startActivity(intent);
    }

    /**
     *
     * @param content
     * @return
     */
    private String isEmpty(EditText content) {

        if (!TextUtils.isEmpty(content.getText().toString().trim())){

            return content.getText().toString().trim();
        }else
            return null;
    }

    /**
     * 获取完善信息
     * @param body
     */
    private void onSuccess(Li_IsUserFinish_Result body) {
        Li_IsUserFinish_Result.DataBean data = body.getData();
        Log.e(TAG, "onSuccess: "+body.getData().toString() );
        //用户名
        if (data.getUi_name()!=null){
            edit_Name.setText(data.getUi_name());
            //setFocuse(edit_Name);
        }
        //邀请码
        if (data.getUi_code()!=null){
            Log.e(TAG, "onSuccess: "+data.getUi_tjrid() );
            edit_invitation.setText(data.getUi_code());
            //setFocuse(edit_invitation);
        }
        //身份证号
        if (data.getUi_cardid()!=null){
            edit_idcard.setText(data.getUi_cardid().toString());
            //setFocuse(edit_idcard);
        }
        //电话号码
        if (data.getUi_phone()!=null){
            edit_phone_pass.setText(data.getUi_phone());
            setFocuse(edit_phone_pass);
        }
        //地址
        if (data.getUi_address()!=null){
            edit_address.setText(data.getUi_address().toString());

        }
        //月收入
        if (data.getUi_income()!=null){
            edit_month_income.setText(data.getUi_income().toString());
        }
        //微信号或者qq
        if (data.getUi_qqwx()!=null){
            edit_weixin.setText(data.getUi_qqwx());
        }
        //借贷额度
        if (data.getUi_limit()!=null){
            borrowing_limit.setText(data.getUi_limit().toString());
            if("2".equals(dataBean.getUi_applying())){
                setFocuse(borrowing_limit);
            }

        }


        //分期数
        //第一位联系人
        if(data.getUi_name1()!=null){
            first_name.setText(data.getUi_name1().toString());
        }
        //第一位联系人电话号码
        if (data.getUi_phone1()!=null){
            first_phone.setText(data.getUi_phone1().toString());
        }
        //第二位联系人
        if (data.getUi_name2()!=null){
            second_name.setText(data.getUi_name2().toString());
        }
        //第二位联系人的电话
        if (data.getUi_phone2()!=null){
            second_phone.setText(data.getUi_phone2().toString());
        }

        //////
        //第二位联系人
        if (data.getUi_name3()!=null){
            third_name.setText(data.getUi_name3().toString());
        }
        //第二位联系人的电话
        if (data.getUi_phone2()!=null){
            third_phone.setText(data.getUi_phone3().toString());
        }

        //第二位联系人
        if (data.getUi_name4()!=null){
            four_name.setText(data.getUi_name4().toString());
        }
        //第二位联系人的电话
        if (data.getUi_phone4()!=null){
            four_phone.setText(data.getUi_phone4().toString());
        }

        //第二位联系人
        if (data.getUi_name5()!=null){
            five_name.setText(data.getUi_name5().toString());
        }
        //第二位联系人的电话
        if (data.getUi_phone5()!=null){
            five_phone.setText(data.getUi_phone5().toString());
        }

        //第二位联系人
        if (data.getUi_name6()!=null){
            six_name.setText(data.getUi_name6().toString());
        }
        //第二位联系人的电话
        if (data.getUi_phone6()!=null){
            six_phone.setText(data.getUi_phone6().toString());
        }

        //支付宝账号
        if (data.getUi_alipay()!=null){
            zfb_et.setText(data.getUi_alipay().toString());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PamaryKeys.RequstSuccess){
            if(resultCode == PamaryKeys.ResultSuccess){
                onFinish();
            }
        }
    }

    /**
     * 设置EditView 是否可以编辑
     * @param view
     */
    private void setFocuse(EditText view){
            view.setFocusable(false);
            view.setFocusableInTouchMode(false);
    }

    private void commitID(Li_IsUserFinish_Result.DataBean result) {
        Map<String,Object> map=  new HashMap<>();

        map.put(PamaryKeys.UI_ID, WriteReadSharedPrefs.readUserID(mContext)+"");
        map.put("ui_name",edit_Name.getText().toString().trim());
        map.put("ui_phone",result.getUi_phone());
        map.put("ui_code",result.getUi_code());
        map.put("ui_tjrid",result.getUi_tjrid());
        map.put("ui_cardid",result.getUi_cardid());
        map.put("ui_province",result.getUi_province());
        map.put("ui_city",result.getUi_city());
        map.put("ui_area",result.getUi_area());
        map.put("ui_address",result.getUi_address());
        map.put("ui_income",result.getUi_income());
        map.put("ui_qqwx",result.getUi_qqwx());
        map.put("ui_name1",result.getUi_name1());
        map.put("ui_name2",result.getUi_name2());
        map.put("ui_phone1",result.getUi_phone1());
        map.put("ui_phone2",result.getUi_phone2());
        map.put("ui_limit",result.getUi_limit());
        map.put("ui_alipay",result.getUi_alipay());

        Log.i("map集合",map.toString());

        apiSevice.onHttp_Info(map).enqueue(new Callback<Li_IsUserFinish_Result>() {
            @Override
            public void onResponse(Call<Li_IsUserFinish_Result> call, Response<Li_IsUserFinish_Result> response) {
                if (response.body().getStatus().equals("success")){
                    start();

                }else {
                    onToast("信息未更新");
                }
            }

            @Override
            public void onFailure(Call<Li_IsUserFinish_Result> call, Throwable t) {
                Log.e("122ee", "onFailure: "+t.getMessage() );
            }
        });
    }
}
